package cn.kgc.bilibili.controller;

import cn.kgc.bilibili.config.AvCode;
import cn.kgc.bilibili.pojo.Video;
import cn.kgc.bilibili.service.VideoService;
import cn.kgc.bilibili.vo.VideoVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 吴厚鹏
 */
@RestController
public class VideoController {
    @Resource
    VideoService videoServiceImpl;

    @GetMapping("/VideoVO/select")
    public VideoVO select(@RequestParam(required = false, name = "title", defaultValue = "") String title) {
        VideoVO videoVO = new VideoVO();
        try {
            List<Video> list = videoServiceImpl.selectVideos(title);
            videoVO.setData(list);
            videoVO.setMsg("查询数据成功");
            videoVO.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            videoVO.setStatus(false);
            videoVO.setMsg(e.getMessage());
        }
        return videoVO;
    }
    @GetMapping("/VideoVO/insert")
    public VideoVO insert(Video video) {
        VideoVO videoVO = new VideoVO();
        AvCode avCode = new AvCode();
        String avid = avCode.createAvid();
        Video videoavid = videoServiceImpl.selectuuid(avid);
        if (videoavid == null) {
            video.setAvid(avid);
            int addvideo = videoServiceImpl.addvideo(video);
            if (addvideo > 0) {
                videoVO.setStatus(true);
                videoVO.setMsg("添加成功");
            } else {
                videoVO.setStatus(false);
                videoVO.setMsg("添加失败");
            }
        } else {
            insert(video);
        }
        return videoVO;
    }

    @GetMapping("/VideoVO/selectype")
    public VideoVO selectype() {
        VideoVO videoVO = new VideoVO();
        try {
            List<Video> list = videoServiceImpl.selecttype();
            videoVO.setData(list);
            videoVO.setMsg("查询数据成功");
            videoVO.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            videoVO.setStatus(false);
            videoVO.setMsg(e.getMessage());
        }
        return videoVO;
    }

    @GetMapping("/VideoVO/selecttg")
    public VideoVO selecttg() {
        VideoVO videoVO = new VideoVO();
        try {
            List<Video> list = videoServiceImpl.selecttg();
            videoVO.setData(list);
            videoVO.setMsg("查询数据成功");
            videoVO.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            videoVO.setStatus(false);
            videoVO.setMsg(e.getMessage());
        }
        return videoVO;
    }

    @GetMapping("/VideoVO/selectyy")
    public VideoVO selectyy() {
        VideoVO videoVO = new VideoVO();
        try {
            List<Video> list = videoServiceImpl.selectyy();
            videoVO.setData(list);
            videoVO.setMsg("查询数据成功");
            videoVO.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            videoVO.setStatus(false);
            videoVO.setMsg(e.getMessage());
        }
        return videoVO;
    }

    @GetMapping("/VideoVO/selectid")
    public VideoVO selectsid(int id) {
        VideoVO videoVO = new VideoVO();
        try {
            Video list = videoServiceImpl.selectid(id);
            videoVO.setVideo(list);
            System.out.println(videoVO.getVideo());
            videoVO.setMsg("查询数据成功");
            videoVO.setStatus(true);
        } catch (Exception e) {
            e.printStackTrace();
            videoVO.setStatus(false);
            videoVO.setMsg(e.getMessage());
        }
        return videoVO;
    }
    @GetMapping("/VideoVO/selectzd")
    public Map<String,Object> selectzd(){
        System.out.println("进入");
        Map<String,Object> map = new HashMap<>();
        List<Video> selectzd01 = new ArrayList<>();
        List<Video> selectzd02 = new ArrayList<>();;
        List<Video> selectzd03 = new ArrayList<>();;
        List<Video> selectzd = videoServiceImpl.selectzd();
        for (int i = 0; i<selectzd.size();i++){
            if(i < 6){
                selectzd01.add(selectzd.get(i));
            }else if (i>5 && i<12){
                selectzd02.add(selectzd.get(i));
            }
            else {
                selectzd03.add(selectzd.get(i));
            }
        }
        map.put("selectzd01",selectzd01);
        map.put("selectzd02",selectzd02);
        map.put("selectzd03",selectzd03);
        return map;
    }
}
